package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysDictData;
import com.blb.community_admin.service.ISysDictDataService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hkp
 */
@RestController
@Api(tags = "字典子模块")
public class SysDictDataController {

    @Resource
    private ISysDictDataService sysDictDataService;

    @ApiOperation("根据字典类型分页查询")
    @GetMapping("/SysDictDatas/page")
    public ResponseResult<IPage<SysDictData>> selectDictDataPage(Integer current, String dictType) {
        return ResponseResult.ok(sysDictDataService.selectDictDataPage(current, 10, dictType));
    }

    /**
     * 模糊分页
     *
     * @param current
     * @param size
     * @param dictName
     * @param dictLabel
     * @param status
     * @return
     */
    @ApiOperation("根据字典名称，字典标签，状态模糊查询")
    @GetMapping("/SysDictDatas/page/fuzzy")
    public ResponseResult<IPage<SysDictData>> selectByFuzzy(Integer current, Integer size, String dictName, String dictLabel, String status) {
        IPage<SysDictData> page = sysDictDataService.selectDictPageByFuzzy(current, size, dictName, dictLabel, status);
        return ResponseResult.ok(page);
    }

    /**
     * 根据dictType查询dictData
     *
     * @param dictType
     * @return
     */
    @ApiOperation("根据dictType对应的dictData")
    @GetMapping("/SysDictData/dictType")
    public ResponseResult<List<SysDictData>> selectAll(String dictType) {
        return ResponseResult.ok(sysDictDataService.list(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictType, dictType)));
    }

    /**
     * 查询所有dictData数据
     *
     * @return
     */
    @GetMapping("/SysDictData")
    public ResponseResult<List<SysDictData>> selectAll() {
        return ResponseResult.ok(sysDictDataService.list(null));
    }

    /**
     * 查询所有listClass
     *
     * @return
     */
    @GetMapping("/sysDictData/listClass")
    public ResponseResult<List<String>> selectAllListClass() {

        List<String> listClassList = sysDictDataService.list(null).stream().map(sysDictData -> sysDictData.getListClass()).filter(listClass -> listClass != null && !"".equals(listClass)).distinct().collect(Collectors.toList());
        return ResponseResult.ok(listClassList);
    }

    /**
     * 新增
     *
     * @param sysDictData
     * @return
     */
    @Log(module = "字典数据",operationType = 1,requestMethod = "POST")
    @PostMapping("/sysDictData")
    public ResponseResult<String> add(@RequestBody SysDictData sysDictData) {
        sysDictData.setCreateTime(LocalDateTime.now());
        SysDictData one = sysDictDataService.getOne(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictLabel, sysDictData.getDictLabel()));
        if (one != null) {
            return ResponseResult.ok("该字典标签已存在，添加失败！");
        }
        sysDictDataService.save(sysDictData);
        return ResponseResult.ok("OK");
    }

    /**
     * 根据数据编码删除
     *
     * @param dictCode
     * @return
     */
    @Log(module = "字典数据",operationType = 3,requestMethod = "DELETE")
    @DeleteMapping("/sysDictData/{dictCode}")
    public ResponseResult<String> deleteById(@PathVariable Long dictCode) {
        sysDictDataService.removeById(dictCode);
        return ResponseResult.ok("OK");
    }

    /**
     * 修改
     *
     * @param sysDictData
     * @return
     */
    @Log(module = "字典数据",operationType = 2,requestMethod = "PUT")
    @PutMapping("/sysDictData")
    public ResponseResult<String> update(@RequestBody SysDictData sysDictData) {
        sysDictData.setUpdateTime(LocalDateTime.now());
        List<SysDictData> list = sysDictDataService.list(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictType, sysDictData.getDictType()));
        boolean flag = list.stream().map(dictData -> dictData.getDictLabel()).anyMatch(a -> a.equals(sysDictData.getDictLabel()));
        if (!flag) {
            return ResponseResult.ok("该字典标签不存在，修改失败！");
        }
        sysDictDataService.updateById(sysDictData);
        return ResponseResult.ok("OK");
    }

    /**
     * 导出
     *
     * @param sysDictDatas
     * @return
     */
    @Log(module = "字典数据",operationType = 5,requestMethod = "PUT")
    @ApiOperation("导出")
    @PostMapping("/sysDictData/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<SysDictData> sysDictDatas) throws IOException {
        String path = EasyExcelUtil.write("字典数据", SysDictData.class, sysDictDatas);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }

    /**
     * 批量删除
     *
     * @param selections
     * @return
     */
    @Log(module = "字典数据",operationType = 3,requestMethod = "DELETE")
    @ApiOperation("根据字典编号批量删除")
    @DeleteMapping("/sysDictType/delete")
    public ResponseResult<String> deleteByIds(@RequestBody List<SysDictData> selections) {
        ArrayList<Long> dictIds = new ArrayList<>();
        selections.stream().forEach(sysDictData -> {
            dictIds.add(sysDictData.getDictCode());
        });
        sysDictDataService.removeByIds(dictIds);
        return ResponseResult.ok("OK");
    }
}
